import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpdateCity {
    public static void main(String[] args) throws SQLException {
        //1.获取数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");
        //2.获得连接
        Connection connection= (Connection) dataSource.getConnection();

        //3.1组装sql 获得执行器
        String updateSql="update city set name=? where id=?";
        PreparedStatement statement=connection.prepareStatement(updateSql);
        //3.2填充占位符
        statement.setString(1,"上海");
        statement.setInt(2,3);

        //4.执行sql
        int result=statement.executeUpdate();
        System.out.println("受影响的行数:"+result);
        //5.关闭资源
        statement.close();
        connection.close();
    }
}
